package com.cube.poiexcel.controller;

import cn.afterturn.easypoi.cache.manager.POICacheManager;
import cn.afterturn.easypoi.excel.ExcelXorHtmlUtil;
import cn.afterturn.easypoi.excel.entity.ExcelToHtmlParams;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * @author ZT
 * @date 2021/5/23 15:53
 */
@Controller
@RequestMapping("tohtml")
public class ExcelToHtmlController {
    Cache<String, String> strCache = CacheBuilder.newBuilder()
            .maximumSize(100) //最大容量
            .expireAfterWrite(31, TimeUnit.DAYS) //缓存过期时长
            .concurrencyLevel(Runtime.getRuntime().availableProcessors())// 设置并发级别为cpu核心数
            .build();

    /**
     * 03 版本EXCEL预览
     */
    @RequestMapping("03")
    public void toHtmlOf03Base(HttpServletResponse response) throws IOException, InvalidFormatException {
        ExcelToHtmlParams params = new ExcelToHtmlParams(WorkbookFactory.create(POICacheManager.getFile("exceltohtml/testExportTitleExcel.xls")));
        response.getOutputStream().write(ExcelXorHtmlUtil.excelToHtml(params).getBytes());
    }

    /**
     * 07 版本EXCEL预览
     * excel转html返回前台
     */
    @RequestMapping("07")
    public void toHtmlOf07Base(HttpServletResponse response) throws IOException, InvalidFormatException, ExecutionException {
//        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = "dept001:sui:" + dateFormat.format(new Date());
        String htmlstring = strCache.getIfPresent(format);
        if (StringUtils.isBlank(htmlstring)) {
            ExcelToHtmlParams params = new ExcelToHtmlParams(WorkbookFactory.create(POICacheManager.getFile("exceltohtml/tmp2.xlsx")));
            htmlstring = ExcelXorHtmlUtil.excelToHtml(params);
            strCache.put(format, htmlstring);
        }

        byte[] bytes = htmlstring.getBytes("UTF-8");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        response.getOutputStream().write(bytes);
    }

    /**
     * 03 版本EXCEL预览
     */
    @RequestMapping("03img")
    public void toHtmlOf03Img(HttpServletResponse response) throws IOException, InvalidFormatException {
        ExcelToHtmlParams params = new ExcelToHtmlParams(WorkbookFactory.create(POICacheManager.getFile("exceltohtml/exporttemp_img.xls")), true, "yes");
        response.getOutputStream().write(ExcelXorHtmlUtil.excelToHtml(params).getBytes());
    }

    /**
     * 07 版本EXCEL预览
     */
    @RequestMapping("07img")
    public void toHtmlOf07Img(HttpServletResponse response) throws IOException, InvalidFormatException {
        ExcelToHtmlParams params = new ExcelToHtmlParams(WorkbookFactory.create(POICacheManager.getFile("exceltohtml/exportTemp_image.xlsx")), true, "yes");
        response.getOutputStream().write(ExcelXorHtmlUtil.excelToHtml(params).getBytes());
    }


}
